Understanding the ::part() Pseudo-Element in CSS
The ::part() pseudo-element allows you to style specific parts of a Web Component's shadow DOM from outside the component. It works in combination with the part attribute inside the component's shadow DOM to expose internal elements for styling.
::part(name) targets elements inside a shadow DOM that have a part="name" attribute.
It allows encapsulated components to expose certain internal elements for styling while keeping the rest of the shadow DOM private.
You can style properties like color, background, padding, border, and more on the exposed part.
Multiple parts can be targeted by separating names with commas, e.g., ::part(header, footer).
In this example, the ::part() pseudo-element is used to style the title and content parts of the <my-card> Web Component. Even though these elements are inside the shadow DOM, they are styled externally without breaking encapsulation.
Use part attributes inside Web Components to expose only the elements intended for external styling.
Combine ::part() with CSS variables for flexible theming and styling.
Avoid exposing unnecessary internal elements to preserve encapsulation.
Test across browsers, as support for ::part() may vary slightly in older versions.
You're building a custom button component with a shadow DOM, and you want the parent page to change the text color of the button's label. How would you expose and style that label using ::part()?
If you forget to add the part attribute to an element inside your shadow DOM, what happens when you try to style it with ::part() from outside?
A team member reports that their custom card component's header isn't responding to ::part(header) styles anymore — the component hasn't changed, but the CSS rule was working last week. What would you check first?
You're building a themable date picker component. You want to let consumers style the selected day and the calendar grid separately. How would you design the part names and what tradeoffs would you consider versus using CSS custom properties?
You're designing a design system with 50+ Web Components that need to support enterprise theming. How would you structure your ::part() API to balance flexibility with maintainability, and what edge cases might break downstream consumers?
A legacy component uses ::shadow and deep selectors for theming. You're migrating to ::part() but some styles are now broken because the internal structure changed. How do you plan the migration without breaking existing themes?
Your company is standardizing on Web Components across 15 product teams. How would you design a governance model for ::part() exposure — what criteria determine whether an internal element gets a part, and how do you handle breaking changes when parts are removed?
You're evaluating whether to adopt ::part() as the primary theming mechanism for your design system. What long-term maintenance, accessibility, and cross-team adoption risks would you surface to leadership, and how would you mitigate them?